import { defineStore } from "pinia";
import type {reqCategoryList} from "@/api/attr"
import  {
    requestgetCategory1,
    requestgetCategory2,
    requestgetCategory3,
} from "@/api/attr"
import { ElMessage } from "element-plus";

//定义state的类型
export interface attrStateType {
    category1List: reqCategoryList,
    category2List: reqCategoryList,
    category3List: reqCategoryList,
    category1Id: null | number,
    category2Id: null | number,
    category3Id: null | number,
}
//创建商品属性列表的小仓库
const useAttrStore = defineStore("attr", {
    state: ():attrStateType  => {
        return {
            category1List: [],
            category2List: [],
            category3List: [],
            category1Id: null,
            category2Id: null,
            category3Id: null,
        }
    },
    actions: {
        //1.请求一级列表
        async getCategory1(){
            try {
                const result = await requestgetCategory1()
                this.category1List = result
            } catch (error) {
                ElMessage.error("请求一级列表失败")
            }
        },
        //1.请求二级列表
        async getCategory2(){
            try {
                const result = await requestgetCategory2(this.category1Id as number)
                this.category2List = result
            } catch (error) {
                ElMessage.error("请求二级列表失败")    
            }
        },
        //3.请求三级列表
        async getCategory3(){
            try {
                const result = await requestgetCategory3(this.category2Id as number)
                this.category3List = result
            } catch (error) {
                ElMessage.error("请求三级列表失败")    
            }
        },
    },
    getters: {},
})

export default useAttrStore